package com.hoshiibuy.customerOperation.service.impl;

import java.time.LocalDateTime;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.customerOperation.dao.ReviewsMapper;
import com.hoshiibuy.customerOperation.entity.Reviews;
import com.hoshiibuy.customerOperation.feign.IdGeneratorService;
import com.hoshiibuy.customerOperation.service.IReviewsService;

/**
 * <p>
 * 平台商品评价 服务实现类
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Service
public class ReviewsServiceImpl extends ServiceImpl<ReviewsMapper, Reviews> implements IReviewsService {

	@Autowired
	private ReviewsMapper reviewsMapper;

	@Autowired
	private IdGeneratorService idGeneratorService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "Reviews" }, allEntries = true)
	@Logable(authName = "前台用户操作-平台商品评价信息管理-新增平台商品评价信息")
	public Response insert(Reviews param) {
		String id = idGeneratorService.queryGeneratorId();
		if (StringUtils.isBlank(id)) {
			throw new ServiceException(CommonError.error("id服务失败"));
		}
		// TODO 判断平台id是否有效
		// TODO 判断用户ID是否有效
		// TODO 判断SPUID是否有效
		// TODO 查看订单是否已评论过
		// TODO 新增追加评论

		param.setId(Long.parseLong(id));
		param.setCreatedtime(LocalDateTime.now());
		int count = reviewsMapper.insert(param);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("新增失败"));
		}
		return Response.success("新增成功");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "Reviews" }, allEntries = true)
	@Logable(authName = "前台用户操作-平台商品评价信息管理-修改平台商品评价信息")
	public Response updateDataById(Reviews param) {
		// TODO 好评不可修改
		// TODO 初始评论在一定时间内可以修改一次
		// TODO 追加评论不可修改
		int count = reviewsMapper.updateById(param);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("修改失败"));
		}
		return Response.success("修改成功");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "Reviews" }, allEntries = true)
	@Logable(authName = "前台用户操作-平台商品评价信息管理-删除平台商品评价信息")
	public Response deleteDataById(String id) {
		// TODO 好评不可删除
		int count = reviewsMapper.deleteById(id);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("删除失败"));
		} else {
			return Response.success("删除成功");
		}
	}

	@Override
	@Cacheable(cacheNames = "Reviews", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
	@Logable(authName = "前台用户操作-平台商品评价信息管理-查询平台商品评价信息详情")
	public Response getDetail(String id) {
		// TODO 查询时一并查询追加评论
		Reviews data = reviewsMapper.selectById(id);
		if (data == null) {
			return Response.failed("数据不存在");
		} else {
			// 根据订单id查询追加评论

			return Response.data(data);
		}
	}

	@Override
	@Cacheable(cacheNames = "Reviews", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5(Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
	@Logable(authName = "前台用户操作-平台商品评价信息管理-查询平台商品评价信息列表")
	public Response getList(QueryWrapper<Reviews> queryWrapper) {
		// TODO 查询时一并查询追加评论
		return Response.data(reviewsMapper.selectList(queryWrapper));
	}

	@Override
	@Cacheable(cacheNames = "Reviews", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
	@Logable(authName = "前台用户操作-平台商品评价信息管理-分页查询平台商品评价信息")
	public Response getPage(IPage<Reviews> page, QueryWrapper<Reviews> queryWrapper) {
		// TODO 查询时一并查询追加评论
		return Response.data(reviewsMapper.selectPage(page, queryWrapper));
	}
}
